package chapter06

/**
  * create by undeRdoG on  2021-06-02  14:30
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Test17_extends {

  def main(args: Array[String]): Unit = {
    // 类型的检测和转换
    val student: Student17 = new Student17("alice",18)
    student.sayHi()
    student.study()

    val person: Person17 = new Student17("bob",10)
    person.sayHi()


    /**
    *   子类的对象可以认为是父类的类型
    * */
    println(student.isInstanceOf[Student17])
    println(student.isInstanceOf[Person17])

    println(person.isInstanceOf[Student17])
    println(person.isInstanceOf[Person17])

    /**
    *  父类的对象不能认为是子类的类型
    * */
    val p2:Person17 = new Person17("Jim",20)
    println(p2.isInstanceOf[Person17])
    println(p2.isInstanceOf[Student17])


    // 类型转换
    if (person.isInstanceOf[Student17]){
      val newStudent = person.asInstanceOf[Student17]
      newStudent.study()
    }

    // 查看类
    println(classOf[Student17])

  }
}


class Person17(var name: String,var age :Int){
  def sayHi():Unit={
    println("hi from person" + name)
  }
}

class Student17(name: String,age :Int) extends Person17(name,age ) {
  override def sayHi():Unit={
    println("hi from student" + name)
  }

  def study()={
    println("student study")
  }
}